/* eslint-disable */
import axios from "axios";
import config from "./config";
import qs from "qs";

// 创建axios实例
const service = axios.create({
  baseURL: `${config.HOST}`,
  timeout: 60000 // 请求超时时间
});

// request拦截器
service.interceptors.request.use(
  async config => {
      let data = config.data;
      config.headers.post["Content-Type"] =
        "application/x-www-form-urlencoded;charset=UTF-8";
      data.uuid = uuid();
      if (config.method === "post") {
        config.data = qs.stringify(config.data);
      }
      return config;
    },
    error => {
      Promise.resolve(error);
    }
);

// respone拦截器
service.interceptors.response.use(
  response => {
    let data = response.data;
    if (data.code == 501) {
      Message({
        message: "服务器维护",
        type: "error"
      });
    }

    return data;
  },
  error => {
    Message({
      message: "服务器错误，请稍后再试",
      type: "error"
    });
    return Promise.reject(error);
  }
);

function uuid() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
    let r = (Math.random() * 16) | 0,
      v = c == "x" ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}

export function post(url, data = {}, config) {
  return new Promise((resolve, reject) => {
    service
      .post(url, data, config)
      .then(
        response => {
          resolve(response);
        },
        err => {
          reject(err);
        }
      )
      .catch(res => {
        Message({
          message: "请求超时，稍后再试",
          type: "error"
        });
      });
  });
}

export function get(url, data = {}) {
  data.uuid = uuid();
  return new Promise((resolve, reject) => {
    service
      .get(url, {
        params: data
      })
      .then(
        response => {
          resolve(response);
        },
        err => {
          reject(err);
        }
      )
      .catch(res => {
        console.log("请求超时", res);
      });
  });
}
export function upload(url, data) {
  return new Promise((resolve, reject) => {
    service
      .post(url, data)
      .then(
        response => {
          resolve(response);
        },
        err => {
          reject(err);
        }
      )
      .catch(res => {
        console.log("请求超时", res);
      });
  });
}